using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
namespace GameStateManagementSample.Screens
{
    class PauseScreen : GameScreen
    {
#region  Fields
        Texture2D m_TPause, m_PauseItem;
        ContentManager m_Content;        
        InputAction pauseAction;
        Rectangle m_BTPlay, m_BTAgain, m_BTMenu;
        
#endregion
        
#region Initialization
        public PauseScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
        }
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (m_Content == null)
                {
                    m_Content = new ContentManager(ScreenManager.Game.Services, "Content");
                    ScreenManager.Game.ResetElapsedTime();
                    m_TPause = m_Content.Load<Texture2D>(@"Image/pause");
                    m_PauseItem = m_Content.Load<Texture2D>(@"Image/PauseItem");

                    m_BTPlay = new Rectangle(90, 300, 200, 120);
                    m_BTAgain = new Rectangle(90, 450, 200, 120);
                    m_BTMenu = new Rectangle(90, 600, 200, 120);

                }
            }
        }
        public override void Deactivate()
        {
            base.Deactivate();
        }
        public override void Unload()
        {
            base.Unload();
        }
#endregion
#region update anhd draw
        protected virtual void OnCancel()
        {
            ExitScreen();            
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
           /* iother = otherScreenHasFocus;
            if (!otherScreenHasFocus)
            {
                UpdateTimeDown(gameTime);
                UpdateScoreDisplay(gameTime);
            }*/

        }
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            PlayerIndex player;
            if (pauseAction.Evaluate(input, ControllingPlayer, out player))
            {
                OnCancel();
                //ScreenManager.AddScreen(new PhonePauseScreen(), PlayerIndex.One);
            }
            TouchCollection touches = input.TouchState;

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    if (m_BTPlay.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        OnCancel();
                    }
                    if (m_BTAgain.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        foreach (GameScreen screen in ScreenManager.GetScreens())
                        {
                            screen.ExitScreen();
                        }
                        ScreenManager.AddScreen(new PlayScreen(), null);
                    }
                    if (m_BTMenu.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        foreach (GameScreen screen in ScreenManager.GetScreens())
                        {
                            screen.ExitScreen();
                        }
                        ScreenManager.AddScreen(new BGScreen(), null);
                        ScreenManager.AddScreen(new MenuScreen(), null);
                    }                   
                }
            }
        }
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();
            spriteBatch.Draw(m_TPause, new Vector2(90,100*(1-TransitionPosition)), Color.White * TransitionAlpha);
            spriteBatch.Draw(m_PauseItem, new Vector2(90,260*(TransitionPosition*2+1)), Color.White * TransitionAlpha);
            spriteBatch.End();
            
        }
#endregion
    }
}
